from SimpleXMLRPCServer import SimpleXMLRPCServer
from Queue import Queue

SERVER = "localhost"
PORT = 8000
queues = {}

# Create server
server = SimpleXMLRPCServer((SERVER, PORT))
server.register_introspection_functions()


class Spqd():

    ### Management methods

    # create new queue
    def create_queue(self, queue, maxsize=0):
        # TODO needs to manage existing queue name
        queues[queue] = Queue(maxsize)
        return True

    # list queues
    def list_queues(self):
        return queues.keys()

    # destroy queue
    def delete_queue(self, queue):
        # manage queue not exists exception
        del queues[queue]
        return True

    ### Queue methods

    # count elements
    def qsize(self, queue):
        return queues[queue].qsize()

    # empty queue
    def empty(self, queue):
        return queues[queue].empty()

    # is queue full
    def full(self, queue):
        return queues[queue].full()

    # put an item in a queue
    def put(self, queue, item, block=True, timeout=None):
        queues[queue].put(item, block, timeout)
        return True

    def put_nowait(self, queue, item):
        return self.put(queue, item, block=False)

    # get an item from a queue
    def get(self, queue, block=True, timeout=None):
        return queues[queue].get(block, timeout)

    def get_nowait(self, queue):
        return self.get(queue, block=False)

    # task_done
    def task_done(self, queue):
        queues[queue].task_done()
        return True

    # join
    def join(self, queue):
        queues[queue].join()
        return True

server.register_instance(Spqd())

# Run the server's main loop
server.serve_forever()
